package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;

import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * @author iTaotao
 * @version 1.0
 * @Description 地址簿接口实现类
 * @createTime 2023年08月03日 09:38:00
 */
@Service
@RequiredArgsConstructor
public class AddressBookServiceImpl implements AddressBookService {
    private final AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> list() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList = addressBookMapper.list();
        return addressBookList;
    }

    @Override
    public void addAddress(AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insertAddressBook(addressBook);


    }

    @Override
    public void setDefault(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        addressBookMapper.updateDefault(addressBook);
    }

    @Override
    public List<AddressBook> selectDefaultAddressBook() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList = addressBookMapper.selectDefaultAddressBook(addressBook);
        return addressBookList;
    }

    @Override
    public void updateAddressBookById(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.updateAddressBookById(addressBook);
    }

    @Override
    public AddressBook selectAddressById(Long id) {
        AddressBook addressBook = addressBookMapper.selectAddressById(id);
        return addressBook;
    }

    @Override
    public void removeAddressById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.deleteAddressById(addressBook);
    }
}
